## ----setup, include=TRUE, results='hide', message = FALSE, warning = FALSE, cache=FALSE--------------------------------------------
library(tidyverse)
library(extraoperators)
library(stringdist)
options(stringsAsFactors = FALSE,
        tibble.print_max = 20, ## if over 20 rows
        tibble.print_min = 5, ## print first 5
        digits = 2) ## reduce digits printed by R


## ----------------------------------------------------------------------------------------------------------------------------------
iris$Species <- as.character(iris$Species)
diris <- as_tibble(iris)
diris


## ----------------------------------------------------------------------------------------------------------------------------------
diris <- arrange(diris, Sepal.Length)
diris


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>% arrange(Sepal.Length)


## ----------------------------------------------------------------------------------------------------------------------------------
diris <- diris %>% arrange(Sepal.Length, desc(Sepal.Width))
diris


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>% anyDuplicated()
anyDuplicated(diris$Sepal.Length)


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>% duplicated() %>% table()

table(duplicated(diris$Sepal.Length))


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>% distinct()


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>% distinct(Sepal.Length)


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>% distinct(Sepal.Length, Sepal.Width)


## ----------------------------------------------------------------------------------------------------------------------------------
slice(diris, 1:5)
diris %>% slice(1:5)


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>% slice(-(1:145))


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>% filter(Species == "setosa")
diris %>% filter(Species != "setosa")


## ----------------------------------------------------------------------------------------------------------------------------------
iris %>% filter(Sepal.Length < 5 & Petal.Width > .2)
diris %>% filter(Sepal.Length == 4.3 | Species != "setosa")


## ----------------------------------------------------------------------------------------------------------------------------------
interest <- c(4.3, 4.4)
diris %>% filter(Sepal.Length %in% interest)


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>% filter(Sepal.Length %nin% interest)


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>% filter(Sepal.Length == 4.3 | Species != "setosa",
                 Petal.Width < 1.5)


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>% select(Sepal.Length, Sepal.Width)

diris %>% select(1, 5, 2)


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>% select("Sepal.Length", "Sepal.Width")


## ----------------------------------------------------------------------------------------------------------------------------------
colVar1 <- c("Sepal.Length", "Sepal.Width")
diris %>% select(all_of(colVar1))


## ----------------------------------------------------------------------------------------------------------------------------------
colVar2 <- c("Sepal.Length", "Sepal.Width", "FAKE!")
diris %>% select(any_of(colVar2))


## ----------------------------------------------------------------------------------------------------------------------------------
head(diris[["Sepal.Length"]]) # easy if you have a R variable
head(diris[[colVar1[1]]]) #easy if you know position
head(diris$Sepal.Length) # easy to type


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>% select(-1)
diris %>% select(-Sepal.Length, -Petal.Width)


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>% select(-colVar1)

diris %>% select(-any_of(colVar2))


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>% select(starts_with("sepal"))
diris %>% select(starts_with("sepal", ignore.case = FALSE))


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>% select(matches("^[[:alpha:]]+[[:punct:]]{1}[[:alpha:]]+"))


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>% names()
diris %>% colnames()


## ----------------------------------------------------------------------------------------------------------------------------------
diris <- diris %>% rename(SepalLength = Sepal.Length)
diris %>% names()


## ----------------------------------------------------------------------------------------------------------------------------------
diris <- diris %>% select(
                SepalLength,
                Petal.Length,
                Sepal.Width,
                Petal.Width,
                Species)
diris


## ----------------------------------------------------------------------------------------------------------------------------------
colVar3 <- c(1, 3, 2, 4, 5)
diris %>% select(all_of(colVar3))


## ----------------------------------------------------------------------------------------------------------------------------------
diris <- as_tibble(iris)
diris %>% mutate(V0 = 0, X1 = 1L, X2 = 2L)


## ----------------------------------------------------------------------------------------------------------------------------------
diris <- mutate(diris, V = Petal.Length * Petal.Width)
diris


## ----------------------------------------------------------------------------------------------------------------------------------
diris <- diris %>% mutate(V2 = if_else(Species == "setosa",
                                       Petal.Length * Petal.Width, NA_real_))
diris <- diris %>% mutate(V2 = if_else(Species == "virginica",
                                       sqrt(Petal.Length * Petal.Width), V2))
diris %>% slice(c(1, 51, 101))


## ----------------------------------------------------------------------------------------------------------------------------------
## computing in summarise allows multiple operations
diris %>% summarise(M = mean(Sepal.Width),
                    SD = sd(Sepal.Width))


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>%
  filter(Species == "virginica") %>%
  summarise(M = mean(Sepal.Width))


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>%
  group_by(Species) %>%
  summarise(
    mSW = mean(Sepal.Width),
    mPW = mean(Petal.Width))


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>%
  group_by(Species) %>%
  summarise(
    mSW = mean(Sepal.Width),
    mPW = mean(Petal.Width)) %>%
  summarise(r = cor(mSW, mPW))


## ----------------------------------------------------------------------------------------------------------------------------------
diris <- diris %>%
  group_by(Species) %>%
  mutate(MedPW = Petal.Width > median(Petal.Width))
diris


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>%
  group_by(Species, MedPW) %>%
  summarise(
    mSW = mean(Sepal.Width),
    mPW = mean(Petal.Width))


## ----------------------------------------------------------------------------------------------------------------------------------
diris %>%
  group_by(Species, MedPW) %>%
  summarise(
    mSW = mean(Sepal.Width),
    mPW = mean(Petal.Width)) %>%
  group_by(MedPW) %>%
  summarise(r = cor(mSW, mPW))


## ----------------------------------------------------------------------------------------------------------------------------------
#Data Set One
diris <- diris %>%
  group_by(Species) %>%
  select(Species, Sepal.Width) %>%
  slice(1:3)
diris

#Data Set TWo
diris2 <- diris %>% slice(c(1, 4, 7))
diris2

#Data Set Three
dalt1 <- tibble(
  Species = c("setosa", "setosa", "versicolor", "versicolor",
              "virginica", "virginica", "other", "other"),
  Type = c("wide", "wide", "wide", "wide",
           "narrow", "narrow", "moderate", "moderate"),
  MedPW = c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE))
dalt1

#Data Set Four
dalt2 <- dalt1 %>% slice(c(1, 3))
dalt2


## ----------------------------------------------------------------------------------------------------------------------------------
diris2 %>% merge(dalt2)


## ----------------------------------------------------------------------------------------------------------------------------------
diris2 %>% merge(dalt2, all.x = TRUE)


## ----------------------------------------------------------------------------------------------------------------------------------
diris2 %>% merge(dalt2, all.y = TRUE)
diris2 %>% merge(dalt2, all = TRUE)


## ----------------------------------------------------------------------------------------------------------------------------------
diris2 %>% left_join(dalt2)


## ----------------------------------------------------------------------------------------------------------------------------------
diris2 %>% right_join(dalt2)


## ----------------------------------------------------------------------------------------------------------------------------------
diris2 %>% inner_join(dalt2)
diris2 %>% anti_join(dalt2)


## ----------------------------------------------------------------------------------------------------------------------------------
diris <- as_tibble(iris)
diris <- diris %>% mutate(ID = 1:n())
diris


## ----------------------------------------------------------------------------------------------------------------------------------
diris.long <-
  diris %>% pivot_longer(
    cols = c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width),
    names_to = c(".value", "Type"),
    names_pattern = "(Sepal|Petal).(Width|Length)"
  )
diris.long


## ----------------------------------------------------------------------------------------------------------------------------------
diris.wide <- diris.long %>% pivot_wider(
  names_from = Type,
  names_sep = ".",
  values_from = c(Sepal, Petal)
)

diris.wide

